﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using ModbusLib;
using ModbusLib.Base;
using 多协议通信平台.Base;

namespace 多协议通信平台.Entity
{
    public partial class ModbusBase : BaseViewModel
    {
        [ObservableProperty]
        private int nodeID;

        [ObservableProperty]
        private EndianType endianType = EndianType.ABCD;

        [ObservableProperty]
        private bool isBaseOne = false;

        private string address;
        public string Address
        {
            get { return address; }
            set
            {
                SetProperty(ref address, value);
                if (address == null) return;
                if (!new char[] { '0', '1', '3', '4' }.Contains(address[0]))
                    throw new Exception("非法的数据存储区");
                StartAddr = ushort.Parse(address.Substring(1));
                switch (address[0])
                {
                    case '0':
                        Region = FunctionType.RCoilStatus;
                        IsWriteable = true;
                        break;
                    case '1':
                        Region = FunctionType.RInputCoil;
                        IsWriteable = false;
                        break;
                    case '3':
                        Region = FunctionType.RHoldingRegister;
                        IsWriteable = true;
                        break;
                    case '4':
                        Region = FunctionType.RInputRegister;
                        IsWriteable = false;
                        break;
                    default:
                        break;
                }
            }
        }

        [ObservableProperty]
        private FunctionType region;

        [ObservableProperty]
        private string dataType;

        [ObservableProperty]
        private object? data;

        [ObservableProperty]
        private ushort startAddr;

        [ObservableProperty]
        public bool isWriteable;
    }
}
